package com.example.projectsharedspace;

import java.util.Random;

public class User {
	
	private String username;
	private int userid;
	private String password;
	private String fName;
	private String lName;
	private String displayname;
	private Home home;
	
	/** 
	 * Constructor
	 * 
	 * @param fname the users first name
	 * @param lname the users last name
	 * @param pw the users selected password
	 * @param userid the server-side generated user id
	 */
	public User(String fname, String lname, String pw, int userid) {
		this.fName = fname;
		this.lName = lname;
		password = pw;
		username = "" + fname.charAt(0) + fname.charAt(1) + lname.charAt(0) + lname.charAt(1) + userid;
		displayname = fname + " " + lname.charAt(0) + ".";
		assert dataInvariant();
	}
	 /**
	  * @return the first (and eventual middle) name(s) of the user.
	  */
	public String getFName() {
		return fName;
	}
	
	/**
	 * @return the last name of the user.
	 */
	public String getLName() {
		return lName;
	}
	
	/**
	 * @return the user name. This name is based on the two first characters of the first name and last name, plus the userid.
	 */
	public String getUN() {
		return username;
	}
	
	/**
	 * @return the password of the user.
	 */
	public String getPW() {
		return password;
	}
	
	/**
	 * @return the id-number of the user. 
	 */
	public int getId() {
		return userid;
	}
	
	/**
	 * @return the displayname of the user, i.e. the first name and the first character of the last name.
	 */
	public String getDisplayname() {
		return displayname;
	}
	
	/**
	 * @return the home in which the user is an inhabitant
	 */
	public Home getHome() {
		return home;
	}
	
	/**
	 * Affiliates the user with a home.
	 * 
	 * @param h the home in which the user is an inhabitant
	 */
	public void setHome(Home h) {
		home = h;
	}
	
	/**
	 * Checks the consistency of the data.
	 * @return whether or not the data is consitent.
	 */
	public boolean dataInvariant() {
		boolean ok = true;
		ok &= fName != null;
		assert ok;
		ok &= fName.length() < 16;
		assert ok : "First name length > 15. Length=" + fName.length();
		ok &= fName.length() > 1;
		assert ok : "First name length < 2. Length=" + fName.length();
		ok &= lName != null;
		assert ok;
		ok &= lName.length() < 16;
		assert ok : "Last name length > 15. Length=" + lName.length();
		ok &= lName.length() > 1;
		assert ok : "Last name length < 2. Length=" + lName.length();
		ok &= password.length() > 4;
		assert ok : "Password length <5. Length=" + password.length();
		ok &= password.length() < 16;
		assert ok : "Password length >15 Length=" + password.length();
		ok &= userid > 0;
		assert ok;
		
		ok &= displayname.charAt(0) == fName.charAt(0) 
				&& displayname.charAt(1) == fName.charAt(1) && displayname.charAt(2) == lName.charAt(0) 
				&& displayname.charAt(3) == lName.charAt(1) && displayname.charAt(4) == userid;
		assert ok : "Displayname not matching requirements. Displayname="+displayname;
		
		return ok;
	}
}
